package br.com.fpf.pontoapp.view.activity;

import roboguice.activity.RoboActivity;
import roboguice.inject.ContentView;
import roboguice.inject.InjectView;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import br.com.fpf.pontoapp.R;
import br.com.fpf.pontoapp.view.util.PontoConstants;

/**
 * The Class LoginActivity.
 *
 * @author hildon.lima
 */
@ContentView(R.layout.login_screen)
public class LoginActivity extends RoboActivity implements OnClickListener {
	
	/** The btn login. */
	@InjectView(R.id.btn_send)
	private transient Button btnLogin;
	
	/** The edt login. */
	@InjectView(R.id.edt_login)
	private transient EditText edtLogin;
	
	/** The edt password. */
	@InjectView(R.id.edt_password)
	private transient EditText edtPassword;
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		super.onResume();
		btnLogin.setOnClickListener(this);
	}

	/* (non-Javadoc)
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		startMainActivity();
	}
	
	/**
	 * Start main activity.
	 */
	private void startMainActivity() {
		if (isValid()) {
			Intent itMainActivity = new Intent(LoginActivity.this, MainActivity.class);
			itMainActivity.putExtra(PontoConstants.LOGIN_IDENTIFIER, edtLogin.getText().toString());
			itMainActivity.putExtra(PontoConstants.PASSWORD_IDENTIFIER, edtPassword.getText().toString());
			startActivity(itMainActivity);
		}
	}	
	
	/**
	 * Checks if is login fields are valid.
	 *
	 * @return true, if is valid
	 */
	private boolean isValid() {
		boolean result = true;
		if ("".equals(edtLogin.getText().toString()) || "".equals(edtPassword.getText().toString())) {
			result = false;
		}
		return result;
	}

}
